package cn.edu.nju.ws.sview.wyge.relations.adataset.entities.qxliu.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import cn.edu.nju.ws.sview.wyge.relations.adataset.entities.qxliu.algorithms.ClassTree;

public class TreeServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		PrintWriter out = response.getWriter();
		String ds_type = request.getParameter("ds_type");
		String idStr = request.getParameter("idStr");
		Integer ds_id = Integer.valueOf(request.getParameter("ds_id"));
		
		// System.out.println("ds_type:"+ds_type+"\tds_id:"+ds_id);

		String state = request.getParameter("state");
		if(state != null){
			response.setContentType("text/xml;charset=utf-8");
			response.setHeader("Charset", "utf-8");
			if (state.contentEquals("0")) {
				Document doc = new ClassTree(ds_type, ds_id).makeDocument();// new
																			// ClassTree().getDocument();
				out.print(doc.asXML());
			} else {
				out.print("not zero! " + state);
			}
			out.flush();
			out.close();
		}else{
			String lang = request.getParameter("lang");
//			response.setContentType("text/json;charset=UTF-8");
			response.setContentType("text/text;charset=UTF-8");
			response.setCharacterEncoding("utf-8");
			response.setHeader("Charset", "UTF8");
			JSONArray tree = null;
			try {
				if(ds_id>0){
					tree = new ClassTree(ds_type, ds_id).makeJSONForDatasetClasses(lang);
				}else if(idStr != null){
					String[] strList = idStr.split("_");
					ArrayList<Integer> docs = new ArrayList<Integer>();
					for(String id :strList){
						if(id.trim().length()>0){
							docs.add(Integer.parseInt(id));
						}
					}
					tree = new ClassTree(docs).makeJSONForDatasetClasses(lang);
				}
				System.out.println("jsonarray  "+tree.toJSONString());
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			JSONObject result = new JSONObject();
//			result.put("tree", tree);
			String t = URLEncoder.encode(tree.toString(),"utf-8").replace("+", "%20");//=============
			result.put("tree", t);
//			System.out.println("result:::"+result.toJSONString());
			response.getWriter().print(result.toString());
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}
